﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using clBUS;
using clEntidad;
using System.IO;
using System.Drawing.Text;

namespace portero_
{
    public partial class frmPrint : Form
    {
        public frmPrint()
        {
            InitializeComponent();
        }
        public int id { get; set; }
        private void frmPrint_Load(object sender, EventArgs e)
        {
            
        }
        private void imprimirRegistro(int id, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                BUSRegistro objBUS = new BUSRegistro(Constantes.CADENA_BD);
                Registro reg = objBUS.get(id);

                //Print bar code
                String idTxt = "I" + id.ToString("0000000");
                e.Graphics.DrawImage(getBarCode(idTxt), 40, 5);
                e.Graphics.DrawString(idTxt, new Font("Roman", 8), Brushes.Black, 175, 60);

                //Print photo
                if (reg.foto != null)
                {
                    MemoryStream ms = new MemoryStream();
                    ms.Write(reg.foto, 0, reg.foto.GetUpperBound(0) + 1);
                    Image foto = System.Drawing.Image.FromStream(ms);
                    ms.Close();

                    e.Graphics.DrawImage(foto, 120, 80, 160, 180);
                }
                //Datos
                e.Graphics.DrawString(reg.persona.apePat + " " + reg.persona.apeMat, new Font("Roman", 11), Brushes.Black, 120, 280);
                e.Graphics.DrawString(reg.persona.nombres, new Font("Roman", 11), Brushes.Black, 120, 300);
                e.Graphics.DrawString("DOC: " + reg.persona.numDoc, new Font("Roman", 11), Brushes.Black, 120, 320);

                //Anfitrion
                Font courier = new Font("Courier new", 10, FontStyle.Regular);
                Font courierN = new Font("Courier new", 10, FontStyle.Bold);
                e.Graphics.DrawString("----------------------------------------", courierN, Brushes.Black, 20, 340);
                e.Graphics.DrawString("Anfitrion:", courier, Brushes.Black, 20, 350);
                e.Graphics.DrawString(reg.persona.visitado.nombre, courierN, Brushes.Black, 20, 365);

                e.Graphics.DrawString("Area     :", courier, Brushes.Black, 20, 385);
                e.Graphics.DrawString(reg.persona.visitado.area, courierN, Brushes.Black, 20, 400);

                e.Graphics.DrawString("Ubicacion:", courier, Brushes.Black, 20, 420);
                e.Graphics.DrawString(reg.persona.visitado.ubicacion, courierN, Brushes.Black, 20, 435);

                e.Graphics.DrawString("Ingreso  : " + reg.entrada, courier, Brushes.Black, 20, 455);

                e.Graphics.DrawString("----------------------------------------", courierN, Brushes.Black, 20, 465);
                e.Graphics.DrawString("------------ E Q U I P O S -------------", courierN, Brushes.Black, 20, 475);
                e.Graphics.DrawString("----------------------------------------", courierN, Brushes.Black, 20, 485);
                int offset = 0;
                foreach (Equipo equipo in reg.equipos)
                {
                    e.Graphics.DrawString(equipo.tipo + " N/S: " + equipo.numSerie, courierN, Brushes.Black, 20, 500 + offset);
                    e.Graphics.DrawString("Marca/Modelo: ", courier, Brushes.Black, 20, 515 + offset);
                    e.Graphics.DrawString(equipo.marca + "/" + equipo.modelo, courierN, Brushes.Black, 20, 530 + offset);
                    e.Graphics.DrawString("----------------------------------------", courierN, Brushes.Black, 20, 540 + offset);
                    offset = offset + 50;
                }
                courier.Dispose();

                e.Graphics.PageUnit = GraphicsUnit.Inch;

            }
            catch (Exception ex)
            {
                Constantes._logger.Error("Error al imprimir", ex);
                MessageBox.Show("Error inesperado, comuníquese con su proveedor");
            }
        }
        private Bitmap getBarCode(String texto)
        {
            texto = "*" + texto + "*";
            Bitmap barCode = new Bitmap(1, 1);
            Graphics graphics = Graphics.FromImage(barCode);
            Font fuente = Varios.getFontCodBar();
            SizeF dataSize = graphics.MeasureString(texto, fuente);
            barCode = new Bitmap(barCode, dataSize.ToSize());
            graphics = Graphics.FromImage(barCode);
            graphics.Clear(Color.White);
            graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
            graphics.DrawString(texto, fuente, new SolidBrush(Color.Black), 5, 0);
            graphics.Flush();
            fuente.Dispose();
            return barCode;
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            imprimirRegistro(this.id, e);
        }


    }
}
